package com.bbms.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bbms.dao.ManagerDao;
import com.bbms.dao.UserDao;
import com.bbms.entity.Manager;
import com.bbms.entity.Page;
import com.bbms.entity.User;
import com.bbms.service.ManagerService;
import com.bbms.service.UserService;

/**
 * 管理员业务层实现类
 * @author Administrator
 */
@Service("ManagerServiceImpl")
public class ManagerServiceImpl implements ManagerService{

	@Resource(name="ManagerDaoImpl")
	private ManagerDao md;
	
	@Resource(name="UserServiceImpl")
	private UserService us;
	@Resource(name="UserDaoImpl")
	private UserDao ud;
	
	/*
	 * 添加管理员
	 * @see com.bbms.service.BaseService#add(java.lang.Object)
	 */
	@Override
	public void add(Manager t) {
		
	}

	/*
	 * 修改管理员信息
	 * @see com.bbms.service.BaseService#update(java.lang.Object)
	 */
	@Override
	public void update(Manager t) {
	
	}

	/*
	 * 删除管理员
	 * @see com.bbms.service.BaseService#delete(java.lang.Object)
	 */
	@Override
	public void delete(Manager t) {
		
	}

	/*
	 * 获取分页数据（无查询）
	 * @see com.bbms.service.BaseService#getPage(int, int)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize) {
		
		return null;
	}

	/*
	 * 插入两个对象
	 * @see com.bbms.service.ManagerService#saveTwoObj(com.bbms.entity.User, com.bbms.entity.Manager)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void saveTwoObj(User user, Manager manager) {
		
		Integer userId;
		us.add(user);
		List<HashMap> list=(List<HashMap>)us.queryByAccount(user.getAccount());
		userId=Integer.parseInt(list.get(0).get("id").toString());
		manager.setUserInfoId(userId);
		md.save(manager);
		
	}

	/*
	 * 更新两个对象
	 * @see com.bbms.service.ManagerService#updateTwoObj(com.bbms.entity.User, com.bbms.entity.Manager)
	 */
	@Override
	public void updateTwoObj(User user, Manager manager) {
		
		System.out.println("更改：==============================");
		System.out.println("用户：   "+ user.toString());
		System.out.println("管理员：   "+ manager.toString());
		System.out.println("==============================");
		
		Integer userId=manager.getUserInfoId();
	
		md.update(manager);
		ud.updateById(user, userId);
		
	}

	/*
	 * 删除两个对象
	 * @see com.bbms.service.ManagerService#delTwoObj(com.bbms.entity.User, com.bbms.entity.Manager)
	 */
	@Override
	public void delTwoObj(User user, Manager manager) {
		
		System.out.println("删除：==============================");
		System.out.println("用户：   "+ user.toString());
		System.out.println("管理员：   "+ manager.toString());
		System.out.println("==============================");
		
		Integer userId=manager.getUserInfoId();
		user.setId(userId);
		md.delete(manager);
		us.delete(user);	
		
	}

	/*
	 * 获取分页数据（含查询）
	 * @see com.bbms.service.ManagerService#getPage(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize, String s_name, String s_account,
			String s_phone, Integer s_categoryId, String IDCard, String start_time, String end_time) {
		
		Page page=new Page();
		
		page=md.getPage(currentPage, pageSize, s_name, s_account, 
				     s_phone, s_categoryId, IDCard, start_time, end_time);
		return page;
	}

}
